

package params;

import java.util.LinkedList;

/**
 * Collects all parameters that can be changed dynamically. If no source provided to get these parameters, default values are used.
 */
public class ParamsController {
    private static byte INDENT = 4;    
    private static byte INDENT_SYMBOL = ' ';    
    private static byte INDENT_STARTER = '{';
    private static byte INDENT_FINISHER = '}';
    private static byte END_OF_LINE = ';';
    private static byte[] LINE_FEED = {'\r','\n'};
    private static byte SPACE = ' ';
    
    private static byte COMMENTARY_PART_1 = '/';
    private static byte COMMENTARY_PART_2 = '*';
    
    private static byte NO_COMMENTARY = -1;
    private static byte COMMENTARY_1ST_TYPE = -2;
    private static byte COMMENTARY_2ND_TYPE = -3;
    
    //getters
    public static byte getIndent(){
        return INDENT;
    }
    public static byte getIndentSymbol(){
        return INDENT_SYMBOL;
    }
    public static byte getIndentStarter(){
        return INDENT_STARTER;
    }
    public static byte getIndentFinisher(){
        return INDENT_FINISHER;
    }
    public static byte getEndOfLine(){
        return END_OF_LINE;
    }
    public static byte[] getLineFeed(){
        return (byte[])LINE_FEED.clone();
    }
    public static byte getSpace(){
        return SPACE;
    }
    public static byte getCommentaryPart1(){
        return COMMENTARY_PART_1;
    }
    public static byte getCommentaryPart2(){
        return COMMENTARY_PART_2;
    }
    public static byte getNoCommentary(){
        return NO_COMMENTARY;
    }
    public static byte getCommentary1stType(){
        return COMMENTARY_1ST_TYPE;
    }    
    public static byte getCommentary2ndType(){
        return COMMENTARY_2ND_TYPE;
    } 
    //setters
    public static void setIndent(byte[] value){
        INDENT = value[0];
    }
    public static void setIndentSymbol(byte[] value){
        INDENT_SYMBOL = value[0];
    }
    public static void setIndentStarter(byte[] value){
        INDENT_STARTER = value[0];
    }    
    public static void setIndentFinisher(byte[] value){
        INDENT_FINISHER = value[0];
    } 
    public static void setEndOfLine(byte[] value){
        END_OF_LINE = value[0];
    } 
    public static void setLineFeed(byte[] value){
        LINE_FEED = value;
    } 
    public static void setSpace(byte[] value){
        SPACE = value[0];
    } 
    public static void setCommentaryPart1(byte[] value){
        COMMENTARY_PART_1 = value[0];
    } 
    public static void setCommentaryPart2(byte[] value){
        COMMENTARY_PART_2 = value[0];
    }
    
    /**
     * Tries to get parameter values from the file with a given file name, if there are no any then leaves standard values 
     * @param fileName - the name of file with parameters
     */
    public static void fillParameters(String fileName){
        LinkedList<byte[]> parsedParams = new LinkedList<byte[]>(); 
        try {
        	PropertyParser propParser = new PropertyParser();
        	parsedParams = propParser.parse(fileName);
        } 
        catch (Exception e) {
            //no property file. OK, let's check if it is a xml file:
            try{
                XmlDomParser xmlParser = new XmlDomParser();
                parsedParams = xmlParser.parseXmlFile(fileName);                
            }
            catch(Exception ex){
               //empty, standard values are accepted
                return;
            }
        }
        try{
	        setIndent(parsedParams.get(0));
	        setIndentSymbol(parsedParams.get(1));
	        setIndentStarter(parsedParams.get(2));
	        setIndentFinisher(parsedParams.get(3));
	        setEndOfLine(parsedParams.get(4));
	        setLineFeed(parsedParams.get(5));
	        setSpace(parsedParams.get(6));
	        setCommentaryPart1(parsedParams.get(7));
	        setCommentaryPart2(parsedParams.get(8));
        }
        catch(IndexOutOfBoundsException e){
        	//not all parameters are present, OK, go ahead with standard values
        }
    }    
}
